package com.shareit.ocr.domain.template;

import java.util.Arrays;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.alibaba.fastjson.JSONObject;
import com.shareit.ocr.domain.JavaScriptEval;
import com.shareit.ocr.domain.SimulatorRequestInfo;
import com.shareit.ocr.util.InvoiceQueryUtil;

/**
 * 增值税普通发票抬头信息模板 Created by geai on 2018-01-21.
 */
public class InvoiceData04HeaderTemplate extends InvoiceDataHeaderTemplate {

	private final static String subfix_04 = "pp";
	private final String rule = "○☺5>4☺878☺-25☺▽";
	public InvoiceData04HeaderTemplate() {
		super(subfix_04);
		InvoiceDataField field;
		field = new InvoiceDataField("fpcc");
		this.fields.add(field);
		field = new InvoiceDataField("fpdm");
		this.fields.add(field);
		field = new InvoiceDataField("fphm");
		this.fields.add(field);
		field = new InvoiceDataField("kprq");
		this.fields.add(field);
		field = new InvoiceDataField("jym");
		this.fields.add(field);
		field = new InvoiceDataField("jqbh");
		this.fields.add(field);
		field = new InvoiceDataField("gfmc");
		this.fields.add(field);
		field = new InvoiceDataField("gfsbh");
		this.fields.add(field);
		field = new InvoiceDataField("gfyhzh");
		this.fields.add(field);
		field = new InvoiceDataField("gfdzdh");
		this.fields.add(field);
		field = new InvoiceDataField("xfmc");
		this.fields.add(field);
		field = new InvoiceDataField("xfsbh");
		this.fields.add(field);
		field = new InvoiceDataField("xfyhzh");
		this.fields.add(field);
		field = new InvoiceDataField("xfdzdh");
		this.fields.add(field);
		field = new InvoiceDataField("je");
		this.fields.add(field);
		field = new InvoiceDataField("se");
		this.fields.add(field);
		field = new InvoiceDataField("jshjdx");
		this.fields.add(field);
		field = new InvoiceDataField("jshjxx");
		this.fields.add(field);
	}

	public void initalFields(SimulatorRequestInfo sri) {
		this.setFieldValue("fpdm", sri.getFpdm());
		this.setFieldValue("fphm", sri.getFphm());
		this.setFieldValue("kprq", sri.getKprq());
		this.setFieldValue("jym", sri.getExt());
	}

	@Override
	public String getExtInfo() {
		String jym = this.getFieldValue("jym");
		return jym.length() > 6?jym.substring(jym.length() -6):jym;
	}

	@Override
	public void setDataFields(Object obj) {

		//web
		if (obj instanceof WebDriver) {
			String value = "";
			WebDriver webdriver = (WebDriver)obj;
			for (InvoiceDataField field :this.getFields()) {
				value = webdriver
						.findElement(By.id(
								this.getSubfixName(field.getName())))
						.getText();
				field.setValue(value);
			}
		}else {
			//API
			String information = (String)obj;
			JSONObject json = JSONObject.parseObject(information);
			String key2 = json.getString("key2");
			String split = Arrays.asList(rule.split("☺")).get(0);
			List<String> keys = Arrays.asList(key2.split(split));
			
			this.setFieldValue("fpcc",InvoiceQueryUtil.getProvince(this.getFieldValue("fpdm"))+ "增值税专用发票");
			this.setFieldValue("jym",keys.get(8));
			this.setFieldValue("jqbh",keys.get(17));
			this.setFieldValue("gfmc",keys.get(14));
			this.setFieldValue("gfsbh",keys.get(6));
			this.setFieldValue("gfyhzh",keys.get(7));
			this.setFieldValue("gfdzdh","");
			this.setFieldValue("xfmc",keys.get(15));
			this.setFieldValue("xfsbh",keys.get(4));
			this.setFieldValue("xfyhzh",keys.get(5));
			this.setFieldValue("xfdzdh",keys.get(1));
			
			JavaScriptEval eval;
			try {
				eval = new JavaScriptEval();
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
//			this.setFieldValue("je",JavaScriptUtil.getje(keys.get(12),rule.split("☺")[2]));
			this.setFieldValue("je","￥" +eval.invokeFunction("getje",keys.get(12),rule.split("☺")[2]));
//			this.setFieldValue("se",JavaScriptUtil.getje(keys.get(16),rule.split("☺")[2]));
			this.setFieldValue("se","￥" + eval.invokeFunction("getje",keys.get(16),rule.split("☺")[2]));
			String hjje = eval.invokeFunction("getje",keys.get(10),rule.split("☺")[2]);
			this.setFieldValue("jshjdx",eval.invokeFunction("NoToChinese",hjje,"04"));
//			this.setFieldValue("jshjxx",JavaScriptUtil.getje(keys.get(10),rule.split("☺")[2]));
			this.setFieldValue("jshjxx","￥" + hjje);
	
		}
	}
}
